package com.huanwei.mainfragment;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.base.BaseActivity;
import com.huanwei.cc.bean.MyEvent;
import com.huanwei.cc.ui.ShareBottomDialog;
import com.huanwei.cc.utils.EmptyUtils;
import com.huanwei.cc.utils.SpfUtils;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 主页面
 *
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.ll_main)
    LinearLayout llMain;
    @BindView(R.id.ll_chat_freely)
    LinearLayout llChatFreely;
    @BindView(R.id.ll_fish_friend_shop)
    LinearLayout llFishFriendShop;
    @BindView(R.id.ll_activity)
    LinearLayout llActivity;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;
    @BindView(R.id.status_bar)
    View topView;
    @BindView(R.id.tab)
   ImageView tab;
    private int currentId = R.id.ll_main;// 当前选中id,默认是主页
    private SY1_Fragment mainFragment;
    private Forum_Fragment chatFragment;
    private Speech3_Fragment activityFragment;
    private Mine4Fragment mine4Fragment;


    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        if (EmptyUtils.isNotEmpty(topView)) {
            mImmersionBar.statusBarView(topView).init();
            mImmersionBar.statusBarDarkFont(true)
                    .init();
        }
    }

    @Override
    public void initViewsAndEvents() {
        EventBus.getDefault().register(this);
        tab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                shareDialog2();
            }
        });
        /**
         * 默认加载首页
         */
        mainFragment = new SY1_Fragment();
        llMain.setSelected(true);// 首页默认选中
        getSupportFragmentManager().beginTransaction().add(R.id.main_container, mainFragment)
                .commit();
    }
    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("finish")) {
                finish();
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    /**
     * 改变fragment的显示
     *
     * @param resId
     */
    private void changeFragment(int resId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//
        // 开启一个Fragment事务

        hideFragments(transaction);// 隐藏所有fragment
        if (resId == R.id.ll_main) {// 主页
            if (mainFragment == null) {// 如果为空先添加进来.不为空直接显示
                mainFragment = new SY1_Fragment();
                transaction.add(R.id.main_container, mainFragment);
            } else {
                transaction.show(mainFragment);
            }
        } else if (resId == R.id.ll_chat_freely) {
            if (chatFragment == null) {
                chatFragment = new Forum_Fragment();
                transaction.add(R.id.main_container, chatFragment);
            } else {
                transaction.show(chatFragment);
            }

        } else if (resId == R.id.ll_activity) {
            if (activityFragment == null) {
                activityFragment = new Speech3_Fragment();
                transaction.add(R.id.main_container, activityFragment);
            } else {
                transaction.show(activityFragment);
            }
        } else if (resId == R.id.ll_mine) {
            String type = (String) SpfUtils.get(this, "type", "1");
            if (!type.equals("1")) {
                if (mine4Fragment == null) {
                    mine4Fragment = new Mine4Fragment();
                    transaction.add(R.id.main_container, mine4Fragment);
                } else {
                    transaction.show(mine4Fragment);
                }
            } else {
                Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
            }
        }
        transaction.commitAllowingStateLoss();// 一定要记得提交事务
    }

    /**
     * 显示之前隐藏所有fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (mainFragment != null)// 不为空才隐藏,如果不判断第一次会有空指针异常
            transaction.hide(mainFragment);
        if (chatFragment != null)
            transaction.hide(chatFragment);
        if (activityFragment != null)
            transaction.hide(activityFragment);
        if (mine4Fragment != null)
            transaction.hide(mine4Fragment);
    }

    /**
     * 改变TextView选中颜色
     *
     * @param resId
     */
    private void changeSelect(int resId) {
        llMain.setSelected(false);
        llChatFreely.setSelected(false);
        llActivity.setSelected(false);
        llMine.setSelected(false);
        switch (resId) {
            case R.id.ll_main:
                llMain.setSelected(true);
                break;
            case R.id.ll_chat_freely:
                llChatFreely.setSelected(true);
                break;
            case R.id.ll_activity:
                llActivity.setSelected(true);
                break;
            case R.id.ll_mine:
                llMine.setSelected(true);
                break;
        }
    }

    @OnClick({R.id.ll_main, R.id.ll_chat_freely, R.id.ll_fish_friend_shop, R.id.ll_activity, R.id
            .ll_mine})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.ll_fish_friend_shop) {

        } else {
            if (v.getId() != currentId) {// 如果当前选中跟上次选中的一样,不需要处理
                changeSelect(v.getId());// 改变图标跟文字颜色的选中
                changeFragment(v.getId());// fragment的切换
                currentId = v.getId();// 设置选中id
            }
        }
    }


    private void shareDialog2() {
        //ShareBottomDialog dialog = new ShareBottomDialog();
        //dialog.show(getSupportFragmentManager());
        ShareBottomDialog dialog = new ShareBottomDialog(this);
        dialog.show( getSupportFragmentManager());
    }
}
